package com.winit.projectPlan.web;

import com.winit.common.base.bean.ExecuteResult;
import com.winit.common.base.web.BaseController;
import com.winit.projectPlan.manager.ProjectPlanMananger;
import com.winit.projectPlan.model.ProjectPlan;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(value = "projectPlan", tags = {"projectPlan"}, description = "projectPlan")
@RestController
@RequestMapping("/api/v1/projectPlan")
public class ProjectPlanController extends BaseController {
    private static Logger log = LoggerFactory.getLogger(ProjectPlanController.class);

    @Autowired
    private ProjectPlanMananger projectPlanMananger;

    @RequestMapping(value = "/getProjectPlans", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取projectPlan")
    @ResponseBody
    public Map getProjectPlans(@ApiParam(value = "projectPlanId，projectPlanId", required = false) @RequestParam(value = "projectPlanId", required = false) String projectPlanId,
                               @ApiParam(value = "containFinished，是否包含已完成的brd,Y-是，N-否（默认）", required = false) @RequestParam(value = "containFinished", required = false) String contain_finished,
                               @ApiParam(value = "userAccount，用户账户", required = false) @RequestParam(value = "userAccount", required = false) String userAccount,
                               @ApiParam(value = "onlineTime，上线时间", required = false) @RequestParam(value = "onlineTime", required = false) Date onlineTime,
                               @ApiParam(value = "keyword，搜索关键字", required = false) @RequestParam(value = "keyword", required = false) String keyword,
                               @ApiParam(value = "开始时间", required = false) @RequestParam(value = "startDate", required = false) Long startDate) {
        String code = BaseController.REQUEST_SUCCESS;
        String desc = BaseController.REQUEST_SUCCESS_MSG;
        /** 进行参数验证 **/
        if (StringUtils.isEmpty(projectPlanId)) {       //防止传空串
            projectPlanId = null;
        }
        if (StringUtils.isEmpty(contain_finished)) {        //默认不包含的brd
            log.debug("contain_finished use default value: N");
            contain_finished = "N";
        } else {    //校验contain_finished只能空、‘Y’，'N‘
            if ((!"Y".equalsIgnoreCase(contain_finished)) && (!"N".equalsIgnoreCase(contain_finished))){
                desc = "illegal arg 'contain_finished', 'Y' or 'N' allowed.. ";
                code = BaseController.REQUEST_ERROR;
                log.error(desc);
                return this.getRtn(code, desc, null);
            }
        }
        ExecuteResult<List<ProjectPlan>> data = this.projectPlanMananger.getProjectPlans(projectPlanId, contain_finished, userAccount, onlineTime, keyword,startDate);
        return this.getRtn(code, desc, data.getData());
    }

    @RequestMapping(value = "/saveProjectPlan", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "保存projectPlan")
    @ResponseBody
    public Map saveProjectPlan(@RequestBody ProjectPlan projectPlan){
        if(null == projectPlan){
            log.error("no params got....");
            return this.getRtn(BaseController.REQUEST_ERROR, "no params got", null);
        }
        if (null != projectPlan.getProjectPlanIsDel() && (!"0".equals(projectPlan.getProjectPlanIsDel())) && (!"1".equals(projectPlan.getProjectPlanIsDel()))){
            return this.getRtn(BaseController.REQUEST_ERROR, "param 'projectPlanIsDel' err, only '0' or '1' allowed", null);
        }
        if (StringUtils.isEmpty(projectPlan.getTokenId())){
            return this.getRtn(BaseController.REQUEST_ERROR, "token id不能为空", null);
        }
        if(null != projectPlan.getProjectPlanOnlineTime() && projectPlan.getProjectPlanOnlineTime().before(new Date())){    //与禅道相关校验保持一致
            return this.getRtn(BaseController.REQUEST_ERROR, "projectPlan上线时间要大于当前时间", null);
        }
        ExecuteResult<ProjectPlan> rs = this.projectPlanMananger.saveProjectPlan(projectPlan);
        Object data = null == rs.getData() ? null : rs.getData().getProjectPlanId();
        return this.getRtn(rs.getCode(), rs.getDesc(), data);
    }

    @RequestMapping(value = "/updateProjectPlanReq", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "新增/删减projectPlan的req")
    @ResponseBody
    public Map updateProjectPlanReq(@ApiParam(value = "projectPlanId，projectPlanId", required = false) @RequestParam(value = "projectPlanId", required = true) String projectPlanId,
                                    @ApiParam(value = "reqIds，reqId以英文逗号分隔", required = false) @RequestParam(value = "reqIds", required = true) String reqIds,
                                    @ApiParam(value = "optMode，添加=1,移除=-1,缺省为-1", required = false) @RequestParam(value = "optMode", required = false,defaultValue = "-1") Integer optMode) {
        List<String> _reqIds = Arrays.asList(reqIds.split(","));

        ExecuteResult data = this.projectPlanMananger.updateProjectPlanReq(projectPlanId, _reqIds, optMode);
        return this.getRtn(data.getCode(), data.getDesc());
    }

    @RequestMapping(value = "/getProjectPlanNo", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取新项目计划编号")
    @ResponseBody
    public Map getProjectPlanNo(){
        return this.getRtn(BaseController.REQUEST_SUCCESS, BaseController.REQUEST_SUCCESS_MSG, ProjectPlan.generateProjectPlanNo());
    }
}
